from concurrent.futures import ThreadPoolExecutor
from blueprints.common import snow_id_creator
from setting import Profixs
import time
pool = ThreadPoolExecutor(max_workers=300)
nums = 0
sets = set()


# 测试python单个线程访问redis能达到1万的访问量，多线程最多可以达到10万的访问量
def voucher_test():
    global nums, sets
    for i in range(300):
        id = snow_id_creator(Profixs.voucher_create_profix)
        nums += 1
        sets.add(id)


start = time.time()
for _ in range(100):  # 受到redis最大连接数影响，最多100个线程连接，超过100的部分没用
    pool.submit(voucher_test)
print(nums, len(sets))  # 3262 3262
time.sleep(1)
print(nums, len(sets))  # 14503 14503
time.sleep(2)
print(nums, len(sets))  # 30000 30000 全局变量先在独立的线程计算，计算完后再累加到主线程
print(time.time() - start)  # 0.32712411880493164
pool.shutdown()

start = time.time()
for _ in range(200):  # 单个线程不受到最大连接数100的影响
    voucher_test()
print(nums, len(sets))  # 90000 90000
print(time.time() - start)  # 5.953710556030273
